#ifndef RENDERCACHEKEY_H
#define RENDERCACHEKEY_H

#include <QtGui>

class RenderCacheKey
{
public:
	RenderCacheKey(int pageNumber = -1, QRect cropRect = QRect(), QSizeF resolution = QSizeF())
		: pageNumber(pageNumber),
		  cropRect(cropRect),
		  resolution(resolution) {
	}

	int pageNumber;
	QRect cropRect;
	QSizeF resolution;

	inline bool operator==(const RenderCacheKey &other) const {
		return pageNumber == other.pageNumber &&
				cropRect == other.cropRect &&
				resolution == other.resolution;
	}
};

QDataStream& operator<<(QDataStream& s, const RenderCacheKey& d);
QDataStream& operator>>(QDataStream& s, RenderCacheKey& d);

inline uint qHash(QRect key)
{
	QByteArray buffer(4 * sizeof(int), 0);
	int x = key.left();
	int y = key.top();
	int w = key.width();
	int h = key.height();
	memcpy(buffer.data() + 0 * sizeof(int), (char*)&x, sizeof(x));
	memcpy(buffer.data() + 1 * sizeof(int), (char*)&y, sizeof(y));
	memcpy(buffer.data() + 2 * sizeof(int), (char*)&w, sizeof(w));
	memcpy(buffer.data() + 3 * sizeof(int), (char*)&h, sizeof(h));
	return qHash(buffer);
}

inline uint qHash(QSizeF key)
{
	QByteArray buffer(2 * sizeof(qreal), 0);
	qreal w = key.width();
	qreal h = key.height();
	memcpy(buffer.data() + 0 * sizeof(qreal), (char*)&w, sizeof(w));
	memcpy(buffer.data() + 1 * sizeof(qreal), (char*)&h, sizeof(h));
	return qHash(buffer);
}

inline uint qHash(const RenderCacheKey &key)
{
	uint h1, h2;
	h1 = qHash(key.pageNumber);

	h2 = qHash(key.cropRect);
	h1 = ((h1 << 16) | (h1 >> 16)) ^ h2;

	h2 = qHash(key.resolution);
	h1 = ((h1 << 16) | (h1 >> 16)) ^ h2;

	return h1;
}

#endif // RENDERCACHEKEY_H
